import 'package:edu/constants/app_strings.dart';
import 'package:edu/networks/course/models/course_response.dart';
import 'package:edu/pages/course/course_detail/course_detail_page_logic.dart';
import 'package:edu/utils/app_theme.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class CourseDetailSummary extends StatelessWidget {
  const CourseDetailSummary({super.key});

  @override
  Widget build(BuildContext context) {
    final bgColor = Theme.of(context).colorScheme.surface;
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
      color: bgColor,
      height: 75,
      child: _buildMainBody(context),
    );
  }

  Widget _buildMainBody(BuildContext context) {
    return Selector<CourseDetailPageLogic, CourseDetail>(
      selector: (context, logic) => logic.courseDetail!,
      builder: (context, v, child) {
        return Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            // 课程等级/难度
            _buildColumnStuff(context, AppStrings.courseLevel, v.levelTitle),
            // 课程时长
            _buildColumnStuff(
              context,
              AppStrings.videoDuration,
              v.durationInHours,
            ),
            // 课程人数
            _buildColumnStuff(
              context,
              AppStrings.studentCount,
              '${v.studentCount}',
            ),
            // 课程评价
            _buildReviewColumn(context, v),
          ],
        );
      },
    );
  }

  // 课程评分
  Widget _buildReviewColumn(BuildContext context, CourseDetail course) {
    var style = Theme.of(context).textTheme.titleSmall;
    final summary = course.reviewSummary;
    if (summary.hasRatings) {
      style = style?.copyWith(fontWeight: FontWeight.bold);
    } else {
      style = style?.copyWith(color: AppTheme.shared.greyColor);
    }

    return Column(
      children: [
        Text(
          AppStrings.courseRating,
          style: Theme.of(context).textTheme.titleMedium,
        ),
        const SizedBox(height: 10),
        Text(
          summary.hasRatings ? summary.ratingString : AppStrings.noRatings,
          style: style,
        ),
      ],
    );
  }

  // 课程信息渲染
  Widget _buildColumnStuff(BuildContext context, String title, String value) {
    return Column(
      children: [
        Text(title, style: Theme.of(context).textTheme.titleMedium),
        const SizedBox(height: 10),
        Text(
          value,
          style: Theme.of(
            context,
          ).textTheme.titleSmall?.copyWith(fontWeight: FontWeight.bold),
        ),
      ],
    );
  }
}
